<template>
  <div class="home-container" ref="containerRef" @wheel="handleWheel" v-loading="loading">
    <ul class="carousel-container" @transitionend="handleTransitionend" :style="{ marginTop }">
      <li v-for="banner in data" :key="banner.id">
        <Carouselitem :carousel="banner" />
      </li>
    </ul>

    <div v-show="index > 0" class="icon icon-up" @click="switchTo(index - 1)">
      <Icon type="arrowUp" />
    </div>

    <div v-show="index < data.length - 1" class="icon icon-down" @click="switchTo(index + 1)">
      <Icon type="arrowDown" />
    </div>

    <ul class="indicator">
      <li :class="{ active: i === index }" @click="switchTo(i)" v-for="(banner, i) in data" :key="banner.id"></li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Carouselitem from './components/Carouselitem.vue'
import Icon from '@/components/Icon'

export default {
  name: 'Home',
  components: { Carouselitem, Icon },
  data() {
    return {
      index: 0,
      containerHeight: 0,
      switching: false,
    }
  },
  computed: {
    ...mapState('banner', ['loading', 'data']),
    marginTop() {
      return -this.index * this.containerHeight + 'px'
    },
  },
  created() {
    this.$store.dispatch('banner/fetchBanner')
  },
  mounted() {
    this.containerHeight = this.$refs.containerRef.clientHeight
    window.addEventListener('resize', this.handleResize)
  },
  destroyed() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.containerHeight = this.$refs.containerRef.clientHeight
    },
    switchTo(i) {
      this.index = i
    },
    handleWheel(e) {
      if (this.switching) {
        return
      }
      if (e.deltaY < -5 && this.index > 0) {
        this.index--
        this.switching = true
      } else if (e.deltaY > 5 && this.index < this.data.length - 1) {
        this.index++
        this.switching = true
      }
    },
    handleTransitionend() {
      this.switching = false
    },
  },
}
</script>

<style scoped lang="less">
@import '~@/styles/mixin.less';
@import '~@/styles/var.less';
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden; // 开启BFC，避免外边距合并
  @gap: 25px;
  .carousel-container {
    width: 100%;
    height: 100%;
    transition: 500ms;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    .self-center();
    color: @gray;
    font-size: 30px;
    cursor: pointer;
    transform: translate(-50%);
    &.icon-up {
      top: @gap;
      animation: jump-up 2s infinite;
    }
    &.icon-down {
      top: auto;
      bottom: @gap;
      animation: jump-down 2s infinite;
    }
    @jump: 5px;
    @keyframes jump-up {
      0% {
        transform: translate(-50%, @jump);
      }
      50% {
        transform: translate(-50%, -@jump);
      }
      100% {
        transform: translate(-50%, @jump);
      }
    }

    @keyframes jump-down {
      0% {
        transform: translate(-50%, -@jump);
      }
      50% {
        transform: translate(-50%, @jump);
      }
      100% {
        transform: translate(-50%, -@jump);
      }
    }
  }
  .indicator {
    .self-center();
    transform: translateY(-50%);
    left: auto;
    right: 20px;
    li {
      width: 7px;
      height: 7px;
      border-radius: 50%;
      background-color: @words;
      cursor: pointer;
      margin-bottom: 10px;
      border: 1px solid #fff;
      box-sizing: border-box;
      transform: 0.5s;
      &.active {
        background-color: #fff;
      }
    }
  }
}
</style>
